#------功能：实现单个会话登录并发帖

from urllib.parse import urlencode

import requests
from requests.cookies import RequestsCookieJar


def writetxt(msg):
    try:
        with open('links1.txt', 'a+', encoding='utf-8') as f:
            f.write(msg + '\n')
    except:
        pass

def run(host,username,password,title,content,userinfo):
    # header 伪装浏览器设置
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Accept-Encoding': 'gzip, deflate, br',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Host': host,
        'X-Real-IP': '225.58.45.24',
        'X-Forwarded-For': '8.8.8.8',
        'Connection': 'keep-alive'
    }

    # 1-----------用Get 获取 网站信息
    # 为请求建立一个会话，保持状态，以后的url都用这个session来get post
    # session 是一个会话，他可以获得现在的cookie headers
    session = requests.session()
    # 给会话添加headers ，不用每次都带上headers了
    session.headers = headers
    # 会话请求 一个url，方式get ，https解除访问屏蔽verify=False
    # html 是会话请求的结果，它可以获得当前的网页内容html.text

    # 2-----------POST登录，postData是form表单内容
    # 设置post提交格式
    postData = {'ecmsfrom': '', 'enews': 'login', 'tobind': '0', 'username': username, 'password': password,
                'lifetime': '99990',
                'Submit': '(unable to decode value)'}

    html1 = session.post(url=f'https://{host}/e/member/doaction.php', data=postData, verify=False)
    print(session.cookies.get_dict())

    # 3----------Post发帖,公告模块
    datas = {
        'spacename': title,
        'spacegg': content,
        'Submit': '(unable to decode value)',
        'enews': 'DoSetSpace'
    }
    url = f'https://{host}/e/member/mspace/index.php'
    # 将form提交数据转换成网页对应合适，本网站的格式gb2312，然后不要用datas直接放到会话请求里边，用转换好的格式data_gb2312来请求
    data_gb2312 = urlencode(datas, encoding='gb2312')
    html2 = session.post(url=url, data=data_gb2312, verify=False)
    # print(html2.text)
    print(session.cookies.get_dict())


    # 4-------------Post发帖,个人信息模块
    datas = {
        'enews': 'EditInfo',
        'truename':'',
        'oicq':'',
        'msn':'',
        'mycall':'',
        'phone':'',
        'homepage':'',
        'userpicfile': '(binary)',
        'address':'',
        'zip':'',
        'saytext': userinfo,
        'Submit': '(unable to decode value)'
    }
    url = f'https://{host}/e/member/doaction.php'
    # 将form提交数据转换成网页对应合适，本网站的格式gb2312，然后不要用datas直接放到会话请求里边，用转换好的格式data_gb2312来请求
    data_gb2312 = urlencode(datas, encoding='gb2312')
    html2 = session.post(url=url, data=data_gb2312, verify=False)
    # print(html2.text)
    print(session.cookies.get_dict())



    class user():
        id = ''
        username = ''

    # 用户的id号码是
    user.id = session.cookies.get_dict()['iwuhbmluserid']
    user.username = session.cookies.get_dict()['iwuhbmlusername']
    print(user)
    writetxt(f'https://{host}/e/space/?userid={user.id}')

    # 4----------退出
    session.get(url=f'https://{host}/e/member/doaction.php?enews=exit', verify=False)
    session.close()

run('www.farpop.com','shenhuawade','1987wade','我们一起出发啊','你要不哟啊一起','这个是我的个人信息')